package com.zmn.oms.zmn.business.impl.orderpay;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.base.channel.common.dro.ChannelDRO;
import com.zmn.base.channel.dubbo.interfaces.channel.ChannelListRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.business.interfaces.conf.channel.ConfOrderChannelBService;
import com.zmn.oms.common.constant.ChannelConsts;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.ProductConsts;
import com.zmn.oms.common.dro.PayChannelDRO;
import com.zmn.oms.third.daowei.DaoweiConsts;
import com.zmn.oms.third.gomegj.GmgjConsts;
import com.zmn.oms.third.tc58.Tc58Consts;
import com.zmn.oms.third.yajule.service.YajuleService;
import com.zmn.oms.zmn.business.interfaces.orderpay.OrderPayChannelBService;
import com.zmn.pay.common.constant.PayConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：工单支付渠道
 *
 * @author heciqi
 * @date 2020/09/28 15:19
 */
@Slf4j
@Service
public class OrderPayChannelBServiceImpl implements OrderPayChannelBService {

	@Autowired
	private YajuleService yajuleService;

	@Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
	private ChannelListRemoteService channelListRemoteService;

	@Resource
	private ConfOrderChannelBService confOrderChannelBService;

	/**
	 * 获取支付渠道
	 * @param platWork 服务平台
	 * @param bizModeSource 下单合同模式
	 * @param channelId 渠道id
	 * @param servProductGroupId 产品分组
	 * @return
	 */
	@Override
	public List<PayChannelDRO> listPayChannel(Integer platWork, Integer bizModeSource, Integer channelId, Integer servProductGroupId) {
		log.info("加载支付渠道OrderPayChannelBService##listPayChannel##platWork[{}]bizModeSource[{}]channelId[{}]", platWork, bizModeSource, channelId);
		// 第三方渠道支付方式
		List<PayChannelDRO> thirdPayChannel = this.listThirdPayChannel(channelId);
		if (CollectionUtil.isNotNullOrEmpty(thirdPayChannel)) {
			return thirdPayChannel;
		}

		// 特殊产品分组-支付渠道
		if (ProductConsts.LIST_SPECIAL_SERV_PRODUCT_GROUP.contains(servProductGroupId)) {
			return this.listSpecialProductGroupChannel();
		}

		List<PayChannelDRO> payChannelDROS = Lists.newArrayList();
		// 平台模式-支付渠道
		if (Objects.equals(bizModeSource, GlobalConsts.BIZ_MODE_PLAT)) {
			payChannelDROS = this.listPlatModelPayChannel(platWork);
		}
		// 自营模式-支付渠道
		else if (Objects.equals(bizModeSource, GlobalConsts.BIZ_MODE_SELF)) {
			payChannelDROS = this.listSelfModelPayChannel(platWork, channelId);
		}

		// 其他支付渠道（翼支付，工行）
		List<PayChannelDRO> otherPayChannel = listOtherPayChannel(channelId);
		if (CollectionUtil.isNotNullOrEmpty(otherPayChannel)) {
			payChannelDROS.addAll(otherPayChannel);
		}
		log.info("返回支付渠道OrderPayChannelBService#listPayChannel====>{}", payChannelDROS);
		return payChannelDROS;
	}

	/**
	 * 是否仅支持银联转账支付
	 *
	 * @param bizModeSource
	 * @param channelId
	 * @return
	 */
	@Override
	public boolean onlySupportBankTransfer(Integer bizModeSource, Integer channelId) {
		if (!Objects.equals(bizModeSource, GlobalConsts.BIZ_MODE_SELF)) {
			return false;
		}

		// 子公司合同，只能银行转账
		ResponseDTO<ChannelDRO> channelRemote = channelListRemoteService.getByChannelId(channelId);
		log.debug("channelListRemoteService#getByChannelId 入参[{}]，出参[{}]", channelId, JSON.toJSONString(channelRemote));
		return channelRemote.isSuccess()
				&& channelRemote.getData() != null
				&& NumberUtil.isNotNullOrZero(channelRemote.getData().getSubCompanyId());
	}

	/**
	 * 获取第三方支付渠道
	 * @param channelId
	 * @return
	 */
	public List<PayChannelDRO> listThirdPayChannel(Integer channelId) {
		
//		if (ChannelConsts.CHANNEL_ID_BAIDU_PAY_ALERT.contains(channelId)) {
//			log.info("订单渠道为：百度, 订单支付:渠道支付.......");
//			PayChannelDRO payChannelDRO = PayChannelDRO.builder()
//					.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_CHANNEL)
//					.payChannelName("百度")
//					.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
//					.build();
//			return Lists.newArrayList(payChannelDRO);
//		}

//		if (ChannelConsts.CHANNEL_ID_GZDCB_PAY_ALERT.contains(channelId)) {
//			PayChannelDRO payChannelDRO = PayChannelDRO.builder()
//					.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_CHANNEL)
//					.payChannelName("贵州多彩宝")
//					.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
//					.build();
//			return Lists.newArrayList(payChannelDRO);
//		}

//		if (ChannelConsts.CHANNEL_ID_58TONGCHENG_PAY_ALERT.contains(channelId)) {
//			log.info("订单渠道为：58同城, 订单支付:渠道支付.......");
//			PayChannelDRO payChannelDRO = PayChannelDRO.builder()
//					.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_CHANNEL)
//					.payChannelName("58同城")
//					.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
//					.build();
//			return Lists.newArrayList(payChannelDRO);
//		}

		if (channelId == OrderConsts.CHANNEL_ID_DJ58_DAG) {
			log.info("订单渠道为：58到家直约API,订单支付:渠道支付.......");
			PayChannelDRO payChannelDRO = PayChannelDRO.builder()
					.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_CHANNEL)
					.payChannelName("58直约")
					.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
					.build();
			return Lists.newArrayList(payChannelDRO);
		}

		// XieWenBing 必须在第三方支付的渠道特殊处理：雅管家平台.....2020/08/05
		// remove at 2021-01-12
		/**
		if (yajuleService.determinYajuleChannel(channelId)) {
			log.info("订单渠道为：雅管家平台渠道,订单支付:渠道支付.......");
			PayChannelDRO payChannelDRO = PayChannelDRO.builder()
					.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_CHANNEL)
					.payChannelName("雅管家渠道")
					.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
					.build();
			return Lists.newArrayList(payChannelDRO);
		}
		 **/

		// XieWenBing 必须在第三方支付的渠道特殊处理：国美管家.....2020/11/09
		if ((Objects.equals(channelId, OrderConsts.CHANNEL_ID_GOME_GJ_JDWX) || Objects.equals(channelId, OrderConsts.CHANNEL_ID_GOME_GJ_ZX))
				&& GmgjConsts.POWER_ON) {
			PayChannelDRO payChannelDRO = PayChannelDRO.builder()
					.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_CHANNEL)
					.payChannelName("微信扫码支付(国美管家)")
					.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
					.build();
			log.info("订单渠道为：国美管家,订单支付:渠道支付.......【{}】", JSON.toJSONString(payChannelDRO));
			return Lists.newArrayList(payChannelDRO);
		}

		// huangyifeng 必须在第三方支付的渠道特殊处理：58同城精选平台.....
		if (Objects.equals(channelId, Tc58Consts.CHANNEL_ID_JX) ||
				Objects.equals(channelId, Tc58Consts.CHANNEL_ID_JX_PIPE) ||
				Objects.equals(channelId, Tc58Consts.CHANNEL_ID_JX_CLOCK) ||
				Objects.equals(channelId, Tc58Consts.CHANNEL_ID_JX_APARTMENT)) {
			log.info("订单渠道为：58同城精选API,订单支付:渠道支付.......");
			PayChannelDRO payChannelDRO = PayChannelDRO.builder()
					.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_CHANNEL)
					.payChannelName("58同城精选")
					.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
					.build();
			return Lists.newArrayList(payChannelDRO);
		}

		// 到位接口系列渠道，新增渠道二维码支付  2020.12.22 by lhl
		if (Objects.equals(channelId, DaoweiConsts.CHANNEL_ID_DTW) ||
				Objects.equals(channelId, DaoweiConsts.CHANNEL_ID_APP) ||
				Objects.equals(channelId, DaoweiConsts.CHANNEL_ID_KYX) ||
				Objects.equals(channelId, DaoweiConsts.CHANNEL_ID_ZX)) {
			log.info("订单渠道为：到位,订单支付:渠道支付.......");
			PayChannelDRO payChannelDRO = PayChannelDRO.builder()
					.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_CHANNEL)
					.payChannelName("到位")
					.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
					.build();
			return Lists.newArrayList(payChannelDRO);
		}

		// 支持渠道收款的标准API对接渠道
		String channelName = ChannelConsts.CHANNEL_OPENAPI_SUPPORT_PAYLINK.get(channelId);
		if (StringUtil.isNotBlank(channelName)) {
			log.info("订单渠道为：{},订单支付:渠道支付.......", channelName);
			PayChannelDRO payChannelDRO = PayChannelDRO.builder()
					.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_CHANNEL)
					.payChannelName(channelName)
					.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
					.build();
			return Lists.newArrayList(payChannelDRO);
		}

		return null;
	}

	/**
	 * 平台模式-支付渠道
	 * @return
	 */
	private List<PayChannelDRO> listPlatModelPayChannel(Integer platWork) {
		List<PayChannelDRO> list = Lists.newArrayList();

		// 银联支付-扫码
		PayChannelDRO eciticPayChannelDRO = PayChannelDRO.builder()
				.payChannelId(PayConsts.PAY_CHANNEL_THIRD_ECITIC)
				.payChannelName(PayConsts.getPayChannelName(PayConsts.PAY_CHANNEL_THIRD_ECITIC))
				.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
				.build();
		list.add(eciticPayChannelDRO);

		// 支付宝-app
		PayChannelDRO aliPayChannelDRO = PayChannelDRO.builder()
				.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_ALIPAY)
				.payChannelName(PayConsts.getPayChannelName(PayConsts.PAY_CHANNEL_PLATFORM_ALIPAY))
				.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_APP))
				.build();
		list.add(aliPayChannelDRO);

		// 微信-app
		if (Objects.equals(platWork, GlobalConsts.PLAT_MARK_ZMN)) {
			PayChannelDRO wexinPayChannelDRO = PayChannelDRO.builder()
					.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_WEIXINPAY)
					.payChannelName(PayConsts.getPayChannelName(PayConsts.PAY_CHANNEL_PLATFORM_WEIXINPAY))
					.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_APP))
					.build();
			list.add(wexinPayChannelDRO);
		}
		return list;
	}

	/**
	 * 自营模式-支付渠道
	 * @return
	 */
	private List<PayChannelDRO> listSelfModelPayChannel(Integer platWork, Integer channelId) {
		List<PayChannelDRO> list = Lists.newArrayList();
		// 支付宝-app，扫码
		PayChannelDRO aliPayChannelDRO = PayChannelDRO.builder()
				.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_ALIPAY)
				.payChannelName(PayConsts.getPayChannelName(PayConsts.PAY_CHANNEL_PLATFORM_ALIPAY))
				.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_APP,PayConsts.PAY_TYPE_QR))
				.build();
		list.add(aliPayChannelDRO);

		// 微信-app，扫码
		List<Integer> payTypeList = Lists.newArrayList(PayConsts.PAY_TYPE_QR);
		if (Objects.equals(platWork, GlobalConsts.PLAT_MARK_ZMN)) {// 啄木鸟平台加载app支付
			payTypeList.add(PayConsts.PAY_TYPE_APP);
		}
		PayChannelDRO wexinPayChannelDRO = PayChannelDRO.builder()
				.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_WEIXINPAY)
				.payChannelName(PayConsts.getPayChannelName(PayConsts.PAY_CHANNEL_PLATFORM_WEIXINPAY))
				.payTypeList(Lists.newArrayList(payTypeList))
				.build();
		list.add(wexinPayChannelDRO);
		return list;
	}

	/**
	 * 特殊产品分组支付支持的支付渠道
	 * @return
	 */
	private List<PayChannelDRO> listSpecialProductGroupChannel() {
		List<PayChannelDRO> list = Lists.newArrayList();
		// 支付宝-app，扫码
		PayChannelDRO aliPayChannelDRO = PayChannelDRO.builder()
				.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_ALIPAY)
				.payChannelName(PayConsts.getPayChannelName(PayConsts.PAY_CHANNEL_PLATFORM_ALIPAY))
				.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_APP,PayConsts.PAY_TYPE_QR))
				.build();
		list.add(aliPayChannelDRO);

		// 微信-扫码
		PayChannelDRO wexinPayChannelDRO = PayChannelDRO.builder()
				.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_WEIXINPAY)
				.payChannelName(PayConsts.getPayChannelName(PayConsts.PAY_CHANNEL_PLATFORM_WEIXINPAY))
				.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
				.build();
		list.add(wexinPayChannelDRO);
		return list;
	}

	/**
	 * 加载其他支付渠道
	 * @param channelId
	 * @return
	 */
	private List<PayChannelDRO> listOtherPayChannel(Integer channelId) {
		// 指定渠道，加载翼支付
		if (Objects.equals(channelId, 10109)) {
			PayChannelDRO yiOrderPayChannelDRO = PayChannelDRO.builder()
					.payChannelId(PayConsts.PAY_CHANNEL_PLATFORM_YIPAY)
					.payChannelName(PayConsts.getPayChannelName(PayConsts.PAY_CHANNEL_PLATFORM_YIPAY))
					.payTypeList(Lists.newArrayList(PayConsts.PAY_TYPE_QR))
					.build();
			return Lists.newArrayList(yiOrderPayChannelDRO);
		}
		return null;
	}
}
